﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DataCentral.Dataset;
using DataCentral.UsecaseControllers;
using DataCentral.BusinessDataObjects;
using CommonData;

namespace DataCentral.Forms
{
    public partial class AccessRightForm : CommonChildForm
    {
        private AccessRightUsecase _UCAccessRight;
        private UserBO _UserBO;
        private bool _IsLoadingRole = false;
        private bool _IsLoadingForm = false;
        public AccessRightForm(AccessRightUsecase _UCAccessRight, UserBO _UserBO)
        {
            InitializeComponent();
            this._UCAccessRight = _UCAccessRight;
            this._UserBO = _UserBO;
        }

        private void AccessRightForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'coffeeManagementDataSet.AccessRight' table. You can move, or remove it, as needed.
            //this.daAccessRight.Fill(this.dsCoffee.AccessRight);
            lsbRole.Items.Clear();
            clbModule.Items.Clear();
            _IsLoadingForm = true;
            daAccessRight.Fill(dsBET.AccessRight);
            ValueListClass clValueList = new ValueListClass();
            lsbRole.DataSource = clValueList.GetRoleList();
            clbModule.DataSource = clValueList.GetModuleList();
            _IsLoadingForm = false;
            lsbRole_SelectedIndexChanged(sender, e);
        }

        private void lsbRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!_IsLoadingForm && lsbRole.SelectedValue != null)
            {
                _IsLoadingRole = true;
                //btnSave_Click(null, e);
                clbModule.UnCheckAll();
                DataRow[] drSelects = dsBET.AccessRight.Select("RoleID = " + lsbRole.SelectedValue.ToString());
                for (int i = 0; i < drSelects.Length; i++)
                {
                    for (int j=0;j<clbModule.ItemCount;j++)
                        if ((decimal)clbModule.GetItemValue(j) == (decimal)drSelects[i]["ModuleID"])
                        {
                            clbModule.SetItemChecked(j,true);
                            break;
                        }
                }
            }
            _IsLoadingRole = false;
        }

        private void clbModule_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            if (!_IsLoadingRole)
            {
                if (!this.Text.EndsWith(ConstantClass._STR_START))
                   this.Text += ConstantClass._STR_START;

                if (e.State == CheckState.Checked)
                {
                    DataCentralDataset.AccessRightRow drAccessRight = dsBET.AccessRight.NewAccessRightRow();
                    drAccessRight.RoleID = (decimal)lsbRole.SelectedValue;
                    drAccessRight.ModuleID = (decimal)clbModule.GetItemValue(e.Index);
                    drAccessRight.CreatedDate = DateTime.Now;
                    drAccessRight.CreatedUserID = _UserBO.UserID;
                    drAccessRight.LastUpdatedDate = DateTime.Now;
                    drAccessRight.LastUpdatedUserID = _UserBO.UserID;
                    dsBET.AccessRight.AddAccessRightRow(drAccessRight);
                }
                else if (e.State == CheckState.Unchecked)
                {
                    foreach (DataCentralDataset.AccessRightRow drSelect in dsBET.AccessRight)
                    {
                        if (drSelect.RoleID == (decimal)lsbRole.SelectedValue && 
                            drSelect.ModuleID == Convert.ToInt32(clbModule.GetItemValue(e.Index)))
                        {
                            dsBET.AccessRight.RemoveAccessRightRow(drSelect);
                            break;
                        }
                    }
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.bsAccessRight.EndEdit();
            daAccessRight.Update(dsBET.AccessRight);
            //this.tableAdapterManager.UpdateAll(this.dsBET);

            string strCaption = this.Text;
            if (strCaption.IndexOf(ConstantClass._STR_START) >= 0)
            {
                this.Text = strCaption.Substring(0, strCaption.IndexOf(ConstantClass._STR_START));
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnCheckAll_Click(object sender, EventArgs e)
        {
            clbModule.CheckAll();
        }

        private void btnUncheckAll_Click(object sender, EventArgs e)
        {
            clbModule.UnCheckAll();
        }

        private void AccessRightForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.Text.IndexOf(ConstantClass._STR_START) >= 0)
            {
                DialogResult dlg = MessageClass.ShowQuestionMessage("Có sự thay đổi dữ liệu ở màn hình này. Bạn có muốn lưu nó trước khi đóng hay không?");
                if (dlg == DialogResult.Yes)
                {
                    btnSave_Click(sender,null);
                }
                else if (dlg == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }
                
            }
        }
    }
}